﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using GeneralUtilities;

namespace GanagroLiteBO
{
    public class PresenterSummaryData
    {
        IreporteView _irv;

        #region Constructors
        private PresenterSummaryData() { }

        public PresenterSummaryData(IreporteView irv)
        {
            _irv = irv;
        }
        #endregion
        public async void SummaryGananciaVida(ReportArgs rArgs)
        {
            string whereClause = WhereClauseBuilder.BuildWhereClause(rArgs);
            string strSql = string.Format("SELECT round(avg(datediff(dd,Fechainicial,fechafinal)),0)," +
                          "isnull(round((1000 * sum(PesoFinal - PesoInicial)) / sum(datediff(dd, Fechainicial, fechafinal)), 0), 0)" +
                          "FROM RepGananciaVida {0}",
                          whereClause
                          );

            List<string> retvals;
            retvals = await Task.Run(() => UtilityDB.GetVals(strSql));
            this._irv.SummaryData1 = string.Format("Prom. Dias : {0} ", retvals[0]);
            this._irv.SummaryData2 = string.Format("G. Diaria : {0} ", retvals[1]);
        }

        public async void VentasVsCompras(ReportArgs rArgs)
        {
            string whereClause = WhereClauseBuilder.BuildWhereClause(rArgs);
            string strSql = string.Empty;

            strSql = string.Format("SELECT round(avg(datediff(dd,Fecha_compra,Fecha_venta)),0)," +
            "isnull(round(1000 * sum(peso_venta - kilos_compra) / sum(datediff(dd, Fecha_compra, Fecha_venta)), 0), 0)," +
            "isnull(round(sum(valor_venta-costo_compra)/sum(datediff(dd,Fecha_compra,Fecha_venta)),0),0) " +
            " FROM VentasVsCompras {0}", whereClause);
            List<string> retvals;
            retvals = await Task.Run(() => UtilityDB.GetVals(strSql));
            this._irv.SummaryData1 = string.Format("Prom. Dias : {0} ", retvals[0]);
            this._irv.SummaryData2 = string.Format("G. Diaria grs : {0} ", retvals[1]);
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
            nfi.CurrencyDecimalDigits = 0;
            this._irv.SummaryData3 = string.Format("G. Diaria (pesos): {0}",
                Convert.ToInt32(retvals[2]).ToString("C", nfi));
        }

        public async void ReporteCebaSummary(ReportArgs rArgs)
        {
            string whereClause = WhereClauseBuilder.BuildWhereClause(rArgs);
            string strSql = string.Format("SELECT Codigo,Peso,Fecha1,Fecha2,Fecha3,UltGan,GVida as [G. Vida],round(PesoHoy,0) as [Peso Hoy]  FROM ANALISIS_CEBA {0} ", whereClause);
            string strSql2 = string.Format("SELECT round(SUM([G. Vida] * datediff(dd,Fecha1,Fecha3)) / sum(datediff(dd,Fecha1,Fecha3)),0)  as f1, " +
            " AVG(datediff(dd,Fecha1,Fecha3)) as f2,round(AVG([Peso Hoy]),0) as f3 from ({0}) TT", strSql);
            List<string> retvals;
            retvals = await Task.Run(() => UtilityDB.GetVals(strSql2));
            this._irv.SummaryData1 = string.Format("G. Poderada: {0}", retvals[0]);
            this._irv.SummaryData2 = string.Format("Dias: {0}", retvals[1]);
            this._irv.SummaryData3 = string.Format("P. Peso: {0}", retvals[2]);
        }

        public async void SummaryGananciasPeriodo(ReportArgs rArgs, string strSqlInnerQuery)
        {
            string strSql = string.Format("select round(Avg(Dias),0),isnull(round((1000 * sum([Peso Final] - [Peso Inicial])) / sum(datediff(dd, [Fecha inicial], [fecha final])), 0), 0) from ({0}) AS T", strSqlInnerQuery);
            List<string> retvals;
            retvals = await Task.Run(() => UtilityDB.GetVals(strSql));
            this._irv.SummaryData1 = string.Format("Prom. Dias: {0} ", retvals[0]);
            this._irv.SummaryData2 = string.Format("G. Diaria : {0} ", retvals[1]);
        }


        public async void SummaryVentas(ReportArgs rArgs)
        {
            string strSql = string.Empty;
            string whereClause = WhereClauseBuilder.BuildWhereClause(rArgs);

            strSql = string.Format("SELECT sum(Kilos)," +
            "round(avg(Kilos),0), " +
            "sum(Precio) FROM Ventas {0}", whereClause);
            List<string> retvals;
            retvals = await Task.Run(() => UtilityDB.GetVals(strSql));
            this._irv.SummaryData1 = string.Format("Total Kilos: {0:G} ", Convert.ToInt32(retvals[0]));
            this._irv.SummaryData2 = string.Format("Prom. Kilos/cabeza: {0:G} ", Convert.ToSingle(retvals[1]));
            this._irv.SummaryData3 = string.Format("Total Venta : {0:C}  ", Convert.ToInt32(retvals[2]));
        }

        public async void SummaryCompras(ReportArgs rArgs)
        {
            string strSql = string.Empty;
            string whereClause = WhereClauseBuilder.BuildWhereClause(rArgs);

            strSql = string.Format("SELECT sum(Kilos)," +
            "round(avg(Kilos),0), " +
            "sum(Precio) FROM Compras {0}", whereClause);
            List<string> retvals;
            retvals = await Task.Run(() => UtilityDB.GetVals(strSql));
            this._irv.SummaryData1 = string.Format("Total Kilos: {0:G} ", Convert.ToInt32(retvals[0]));
            this._irv.SummaryData2 = string.Format("Prom. Kilaje: {0:G} ", Convert.ToSingle(retvals[1]));
            this._irv.SummaryData3 = string.Format("Total Compra : {0:C}  ", Convert.ToInt32(retvals[2]));
        }

        public async void SummaryMvtos(ReportArgs rArgs)
        {
            string strSql = string.Empty;
            string whereClause = WhereClauseBuilder.BuildWhereClause(rArgs);

            strSql = string.Format("SELECT sum(TOTAL_INV)" +
            " FROM RepMovimientos {0}", whereClause);

            List<string> retvals;
            retvals = await Task.Run(() => UtilityDB.GetVals(strSql));
            this._irv.TotalSemovientes = string.Format("Delta : {0} cabezas.", retvals[0]);
            this._irv.SummaryData1 = string.Empty;
            this._irv.SummaryData2 = string.Empty;
            this._irv.SummaryData3 = string.Empty;
        }

        public async void SummaryPesajes(ReportArgs rArgs)
        {
            string strSql = string.Empty;
            string whereClause = WhereClauseBuilder.BuildWhereClause(rArgs);

            strSql = string.Format("SELECT count(*)" +
            " FROM Pesajes {0}", whereClause);

            List<string> retvals;
            retvals = await Task.Run(() => UtilityDB.GetVals(strSql));
            this._irv.TotalSemovientes = string.Format("Total {0} .", retvals[0]);
            this._irv.SummaryData1 = string.Empty;
            this._irv.SummaryData2 = string.Empty;
            this._irv.SummaryData3 = string.Empty;
        }

 
        public async void SetRepPRV(ReportArgs rArgs)
        {
            string strSql = string.Empty;
            string whereClause = WhereClauseBuilder.BuildWhereClause(rArgs);

            strSql = string.Format("SELECT count(*)" +
            " FROM Analisis_PRV {0}", whereClause);

            List<string> retvals;
            retvals = await Task.Run(() => UtilityDB.GetVals(strSql));

            this._irv.TotalSemovientes = string.Format("Semovientes pesados: {0} .", retvals[0]);
            this._irv.SummaryData1 = string.Empty;
            this._irv.SummaryData2 = string.Empty;
            this._irv.SummaryData3 = string.Empty;
        }




    }
}

